from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from datetime import date
from typing import List, Dict, Optional
import matplotlib.pyplot as plt
import numpy as np
import os
from io import BytesIO
import base64
from fastapi.responses import JSONResponse
from service.learning_plan_service import *


app = FastAPI()

# 规划成绩不记录数据库
exam_data = []
goal_data = []
total_data = []

@app.post("api/exams/")
async def add_exam_record(data): 
    """添加考试记录
        参数 例: {
            "recent_scores": [
                {"date": "2025-11-15", "scores": {"数学": 85, "英语": 92, "物理": 78}},
                {"date": "2025-12-10", "scores": {"数学": 88, "英语": 90, "物理": 82}}
            ],
            "goal_score": {"数学": 95, "英语": 95, "物理": 90}
            }
    """
    exam_data.append(data[0])
    goal_data.append(data[-1])
    total_data.append(data)

    return {"code": 200, "message":"Exams record added successfully"}


@app.get("api/exams/analysis")
async def get_student_exams():
    """获取趋势图"""
    data = exam_data[1]
    return {'code':200,'message':plot_scores_trend(data)}


@app.get("api/analysis/objects")
async def analyze_subject():
    """分析特定科目成绩并提供学习建议"""
    data = find_weak_subjects(total_data[1])
    if data :
        return {'code':200,'weak objection':data}

@app.get("api/analysis/videos")
async def videos():
    ''' 给薄弱的科目的相关视频'''
    data = []
    subjects = find_weak_subjects(total_data[1])
    for i in subjects :
        a = i['subject']
        data.append(a)

    data = generate_bilibili_search_urls(data)
    return {'code':200,'message':data}


 
